package com.kehutong.admin.controler;

import com.kehutong.admin.entity.Invoice;
import com.kehutong.admin.entity.db.CorpDatabase;
import com.kehutong.common.DoveClient;
import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.threadpool.ThreadPool;

import com.kehutong.admin.entity.Corp;
import com.kehutong.admin.service.AuthService;
import com.kehutong.common.util.Token;

@MvcAction
@ReqMapping("/admin/sys/corp")
public class CorpController {

    @Inject
    private JdbcSession jdbcSession;
    @Inject
    private AuthService authService;
    @Inject
    private ThreadPool threadPool;
    @Inject
    private DoveClient doveClient;

    private final static String ID = "162159317073429801";

    @HasPermission("sys:corp:view")
    @ReqMapping("/list")
    public Object list(JSONObject jsonObject) {
        return jdbcSession.findPage(Corp.class)
        		.eq(jsonObject, "id")
				.like(jsonObject, "name", "fullName")
				.eq("deleted", false)
				.page(jsonObject)
				.exe();
    }

    @ReqMapping("/currentCorp")
	public Object currentCorp(Token token) throws Exception {
		Corp corp = jdbcSession.findOne(Corp.class)
				.eq("id", token.getCompanyNo())
				.eq("deleted", false)
				.exe();

        Invoice invoice = Pool.get(Invoice.class, token.getCompanyNo());

        JSONObject result = new JSONObject();
        result.put("corp", corp);
        result.put("invoice", invoice);

       return Result.success(result);
	}

    @HasPermission("sys:corp:view")
    @ReqMapping("/get")
    public Object getById(Corp corp) {
        return corp;
    }

    @Bean(newInstance = true, copy = true)
    @HasPermission("sys:corp:edit")
    @ReqMapping("/save")
    public Object save(Token token, Corp corp) {
    	corp.setUpdate_by(token.getUuid());
    	corp.setCreate_by(token.getUuid());
    	jdbcSession.insert(corp);
    	
    	/*threadPool.execute(()->{
    		authService.createDatabase(corp);
    	});*/
    	
        return corp;
    }

    @Bean(copy = true)
    @HasPermission("sys:corp:edit")
    @ReqMapping("/update")
    public Object update(Token token, Corp corp) {
    	corp.setUpdate_by(token.getUuid());
    	jdbcSession.updateById(corp);
        return corp;
    }

    @HasPermission("sys:corp:edit")
    @ReqMapping("/delete")
    public Object deleteById(Token token, Corp corp) {
    	
    	corp.setDeleted(true);
    	corp.setUpdate_by(token.getUsername());
    	jdbcSession.updateById(corp);


        return Result.success();
    }

    @HasPermission("sys:corp:edit")
    @ReqMapping("/dropCorpDatabase")
    public Object dropCorpDatabase(String id) throws Exception {

        // 不允许删除悠悠投资
        if(ID.equals(id)){
            return Result.success();
        }

        // delete from t_user where 1=1 and corpNo='';
        doveClient.post("/kht-bin/auth/user/deleteUser", http-> {
            http.addParam("corpNo", id);
        });

        // delete from t_wwout_agent where 1 = 1 and authCorpid = '';
        // delete from t_wwout_corp where 1=1 and corpNo='';
        doveClient.post("/kht-bin/wwout/agent/deleteAgentData", http->{
            http.addParam("id", id);
        });

        authService.dropDatabase(id);

        return Result.success();
    }
    
//    @ReqMapping("/createdb")
//    public Object createdb(Corp corp) throws Exception {
//    	AuthService authService = Ioc.getInstance(AuthService.class);
//    	authService.createDatabase(corp);
//
//        return Result.success();
//    }
}
